package com.yinNuo.collectWool.mobileApi.collectWoolApi;

import com.yinNuo.collectWool.mobileApi.entity.UserDoTask;
import com.yinNuo.collectWool.mobileApi.service.UserDoTaskService;
import com.yinNuo.collectWool.mobileApi.vo.UserDoTaskVo;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 用户做任务详细表 所有数据都是从这里判断(UserDoTask)表控制层
 *
 * @author makejava
 * @since 2021-12-12 11:00:40
 */
@RestController
@RequestMapping("/userDoTask")
public class UserDoTaskApi {
    /**
     * 服务对象
     */
    @Resource
    private UserDoTaskService userDoTaskService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<UserDoTask> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.userDoTaskService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param userDoTaskVo 实体
     * @return 新增结果
     */
    @PostMapping("/add")
    public ResponseEntity<UserDoTask> add(@RequestBody UserDoTaskVo userDoTaskVo) {
        UserDoTask userDoTask = new UserDoTask();
        String userId = userDoTaskVo.getUserId().split(":")[1].split("-")[0];
        userDoTask.setUserId(Integer.parseInt(userId));
        String woolTaskId = userDoTaskVo.getWoolTaskId().split(":")[1].split("-")[0];
        userDoTask.setWoolTaskId(Integer.parseInt(woolTaskId));
        String woolTaskDetailId = userDoTaskVo.getWoolTaskDetailId().split(":")[1].split("-")[0];
        userDoTask.setWoolTaskDetailId(Integer.parseInt(woolTaskDetailId));
        userDoTask.setToken(userDoTaskVo.getToken());
        return ResponseEntity.ok(this.userDoTaskService.insert(userDoTask));
    }

    /**
     * 编辑数据
     *
     * @param userDoTask 实体
     * @return 编辑结果
     */
    @PutMapping("/edit")
    public ResponseEntity<UserDoTask> edit(UserDoTask userDoTask) {
        return ResponseEntity.ok(this.userDoTaskService.update(userDoTask));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/deleteById")
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.userDoTaskService.deleteById(id));
    }

}

